package view.component.base;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;

import util.MyButton;
import view.ComponentWindow;
import view.component.basecomponent.pojo.ComponentPojo;

import component.util.BaseMotionAdapter;

import control.Supervisorio;
import entities.Variable;

@SuppressWarnings("serial")
public class BaseComponent extends MyButton {

	private JPopupMenu popupComponent = new JPopupMenu();
	private JMenuItem menuComponentRename = new JMenuItem("Renomear...");
	private JMenuItem menuComponentExclude = new JMenuItem("Excluir");
	private JMenuItem menuComponentProperties = new JMenuItem("Propriedades...");
	private BasePanel basePanel;
	private ComponentPojo pojo;
	private String imgAddress;
	private Supervisorio supervisorio;

	public BaseComponent(BasePanel panel, String fileAdress, MouseEvent evt) {
		super(panel, fileAdress);
		super.setLocation(evt.getX(), evt.getY());// TODO ALTERAR SUPER PARA
		this.setBackground(Color.WHITE);

		this.supervisorio = Supervisorio.getInstance();
		this.imgAddress = fileAdress;

		panel.add(this);
		new BaseMotionAdapter(this, panel);
		this.basePanel = panel;
		this.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				componentMouseClicked(evt);
			}
		});

		menuComponentRename.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuComponentRenameActionPerformed();
				supervisorio.remoteVariableBuildTree();
			}
		});
		menuComponentExclude.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuComponentExcludeActionPerformed();
				supervisorio.remoteVariableBuildTree();
			}
		});
		menuComponentProperties.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuComponentPropertiesActionPerformed();
				supervisorio.remoteVariableBuildTree();
			}
		});
		popupComponent.add(menuComponentRename);
		popupComponent.add(menuComponentExclude);
		popupComponent.add(menuComponentProperties);
	}

	private void menuComponentRenameActionPerformed() {
		String text = JOptionPane.showInputDialog("Renomear...");
		pojo.setName(text);
		this.repaint();
	}

	private void menuComponentExcludeActionPerformed() {
		String[] options = { "Ok", "Cancelar" };
		int result = JOptionPane.showOptionDialog(null, "Você tem certeza?",
				"Excluir", JOptionPane.OK_CANCEL_OPTION,
				JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
		if (result == 0) {
			try {
				basePanel.remove(this);
				basePanel.repaint();
				supervisorio.deleteComponent(this.pojo);
				this.finalize();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
	}

	private void componentMouseClicked(MouseEvent evt) {

		if (supervisorio.isSimulationRunning()) {
			supervisorio.showVariablesOf(this.pojo);
		} else if (evt.getClickCount() == 2) {
			this.montaJanela();
		} else if (evt.getButton() == evt.BUTTON3) {
			popupComponent.show(this, evt.getX(), evt.getY());
		}
	}

	private void menuComponentPropertiesActionPerformed() {
		this.montaJanela();
	}

	public void montaJanela() {
		ComponentWindow frame = new ComponentWindow(this);

		frame.setVisible(true);
	}

	public void addVar(Variable variable) {
		pojo.getVariables().add(variable);
	}

	public int getNumberOfVariables() {
		return pojo.getVariables().size();
	}

	public ArrayList<Variable> getVariables() {
		Collection<Variable> variables = pojo.getVariables();

		if (variables != null)
			return new ArrayList<Variable>(variables);
		else
			return new ArrayList<Variable>();
	}

	public Dimension getPreferredSize() {
		return new Dimension(100, 50);
	}

	public boolean isOpaque() {
		return true;
	}

	public void setLocation(int x, int y) {
		super.setLocation(x, y);
		pojo.setX(x);
		pojo.setY(y);
	}

	public BasePanel getPanel() {
		return basePanel;
	}

	public void setPanel(BasePanel panel) {
		this.basePanel = panel;
	}

	public ComponentPojo getPojo() {
		return pojo;
	}

	public void setPojo(ComponentPojo pojo) {
		this.pojo = pojo;
	}

	public String getImgAddress() {
		return imgAddress;
	}

	public void setImgAddress(String imgAddress) {
		this.imgAddress = imgAddress;
	}
}
